import subprocess
import lit.util


def can_execute_generated_snippets(arch):
    is_host_arch = arch in config.root.host_triple
    # 'native' feature is defined as "host arch == default triple arch"
    is_native_codegen = 'native' in config.available_features
    return is_host_arch and is_native_codegen


def can_use_perf_counters(mode, extra_options=[]):
    # We need libpfm to be installed and allow reading perf counters. We can
    # only know that at runtime, so we try to measure an empty code snippet
    # and bail out on error.
    llvm_exegesis_exe = lit.util.which('llvm-exegesis', config.llvm_tools_dir)
    if llvm_exegesis_exe is None:
       print('could not find llvm-exegesis')
       return False
    try:
        return_code = subprocess.call(
            [llvm_exegesis_exe, '-mode', mode, '-snippets-file', '/dev/null'] + extra_options,
            stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
        return return_code == 0
    except OSError:
        print('could not exec llvm-exegesis')
        return False


for arch in ['aarch64', 'mips', 'powerpc', 'x86_64']:
    if can_execute_generated_snippets(arch):
        config.available_features.add('exegesis-can-execute-%s' % arch)

if can_use_perf_counters('latency'):
    config.available_features.add('exegesis-can-measure-latency')

if can_use_perf_counters('uops'):
    config.available_features.add('exegesis-can-measure-uops')

if can_execute_generated_snippets('x86_64'):
    # Check for support of LBR format with cycles.
    if can_use_perf_counters('latency', ['-x86-lbr-sample-period', '123', '-repetition-mode', 'loop']):
        config.available_features.add('exegesis-can-measure-latency-lbr')
